package com.zheng.oa.admin.controller.hr;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.zheng.common.base.BaseController;
import com.zheng.common.validator.IntValidator;
import com.zheng.common.validator.LengthValidator;
import com.zheng.hr.dao.model.HrPersonalContract;
import com.zheng.hr.rpc.api.HrDictService;
import com.zheng.hr.rpc.api.HrPersonalContractRecordsService;
import com.zheng.hr.rpc.api.HrPersonalContractService;
import com.zheng.oa.common.constant.OaResult;
import com.zheng.oa.common.constant.OaResultConstant;
import com.zheng.oa.common.exception.OaException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by IntelliJ IDEA.
 * User: xinqi
 * Date: 2018/1/15
 * Time: 14:04
 */
@Controller
@Api(value = "人事合同管理", description = "人事合同管理")
@RequestMapping("/oa/hr/personalContract")
public class HrPersonalContractController extends BaseController {
    @Autowired
    private HrPersonalContractService contractService;
    @Autowired
    private HrPersonalContractRecordsService contractRecordsService;
    @Autowired
    private HrDictService dictService;
    //默认最大合同年份
    private final static int DEFAULT_MAX_PERIOD = 10;

    @ApiOperation(value = "签订人事合同")
    @RequiresPermissions("oa:hr:personalContract:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Integer id, Model model) {
        model.addAttribute("personalId", id);
        return "/manage/hr/personalRecords/createContract.jsp";
    }

    @ApiOperation(value = "签订人事合同")
    @RequiresPermissions("oa:hr:personalContract:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(@RequestBody HrPersonalContract personalContract) {
        String dictValue = dictService.getDictValue("年份", "max_period",  String.valueOf(DEFAULT_MAX_PERIOD));
        int period = 0;
        try {
            period = Integer.parseInt(dictValue);
        } catch (NumberFormatException e) {
            period = DEFAULT_MAX_PERIOD;
        }
        ComplexResult result = FluentValidator.checkAll()
                .on(personalContract.getPeriod(), new IntValidator(0, period, "合同期限"))
                .on(personalContract.getContractNumber(), new LengthValidator(0, 100, "合同编号"))
                .on(personalContract.getRemarks(), new LengthValidator(0, 1000, "备注"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new OaResult(OaResultConstant.FAILED, result.getErrors().get(0).getErrorMsg());
        }
        try {
            if (!contractService.insertPersonalContract(personalContract, getUsername())) {
                return new OaResult(OaResultConstant.FAILED, null);
            }
        } catch (OaException e) {
            return new OaResult(OaResultConstant.FAILED, e.getMessage());
        }
        return new OaResult(OaResultConstant.SUCCESS, null);
    }


    @ApiOperation(value = "修改人事合同")
    @RequiresPermissions("oa:hr:personalContract:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@RequestBody HrPersonalContract personalContract) {
        String dictValue = dictService.getDictValue("年份", "max_period",  String.valueOf(DEFAULT_MAX_PERIOD));
        int period = 0;
        try {
            period = Integer.parseInt(dictValue);
        } catch (NumberFormatException e) {
            period = DEFAULT_MAX_PERIOD;
        }
        ComplexResult result = FluentValidator.checkAll()
                .on(personalContract.getPeriod(), new IntValidator(0, period, "合同期限"))
                .on(personalContract.getContractNumber(), new LengthValidator(0, 100, "合同编号"))
                .on(personalContract.getRemarks(), new LengthValidator(0, 1000, "备注"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new OaResult(OaResultConstant.FAILED, result.getErrors().get(0).getErrorMsg());
        }
        try {
            if (!contractService.updatePersonalContract(personalContract)) {
                return new OaResult(OaResultConstant.FAILED, null);
            }
        } catch (OaException e) {
            return new OaResult(OaResultConstant.FAILED, e.getMessage());
        }
        return new OaResult(OaResultConstant.SUCCESS, null);
    }
}
